package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.domain.*;
import com.pureut.storage.domain.order.domain.ConverterManagement;
import com.pureut.storage.dto.IncomingMaterialPrintingDetailDto;
import com.pureut.storage.dto.IncomingMaterialPrintingDto;
import com.pureut.storage.dto.UnitConversionDto;
import com.pureut.storage.mapper.*;
import com.pureut.storage.service.*;
import com.pureut.storage.util.UnitUtil;
import com.pureut.storage.vo.DeleteDetailVo;
import com.pureut.storage.vo.IncomingMaterialPrintingVo;
import com.pureut.storage.vo.IncomingMaterialSubcontractPrintingVo;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.core.domain.R;
import com.sale.common.core.exception.GlobalException;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.common.satoken.utils.LoginHelper;
import com.sale.order.api.RemoteSupplierService;
import com.sale.order.api.dto.OrderSupplierManagementDto;
import com.sale.system.api.RemoteCodingService;
import com.sale.system.api.RemoteSysConverterService;
import com.sale.system.api.RemoteSysSupplierService;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.dto.SysApiCategoryDto;
import com.sale.system.api.domain.dto.SysApiOrderSupplierManagementDto;
import com.sale.system.api.domain.dto.SysConverterManagementDto;
import com.sale.system.api.domain.dto.SysMaterialCodingResultDto;
import com.sale.system.api.model.MaterialUnitDto;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * 来料打印明细实现层
 * Author:C
 * Date:2022/12/27 11:56
 * @DESC
 */
@Service
@RequiredArgsConstructor
public class IncomingMaterialPrintingDetailServiceImpl implements IncomingMaterialPrintingDetailService {


    private final IncomingMaterialPrintingDetailMapper incomingMaterialPrintingDetailMapper;


    private final IncomingMaterialPrintingMapper incomingMaterialPrintingMapper;

    private final PurchaseReceiptDetailMapper purchaseReceiptDetailMapper;

    private final PurchaseReceiptMapper purchaseReceiptMapper;

    private final WmsStockInfoMapper wmsStockInfoMapper;

    @DubboReference
    RemoteSysSupplierService remoteSysSupplierService;

    @DubboReference
    RemoteSysConverterService remoteSysConverterService;

    @DubboReference
    RemoteUnitService remoteUnitService;

    @DubboReference
    RemoteCodingService remoteCodingService;

    private final IncomingMaterialSubcontractPrintingMapper incomingMaterialSubcontractPrintingMapper;

    private final IncomingMaterialPrintingService incomingMaterialPrintingService;

    private final UnitUtil unitUtil;

    private final SubcontractReceiptDetailService subcontractReceiptDetailService;

    private final WipCompletionDetailService wipCompletionDetailService;

    private final SubcontractReceiptDetailMapper subcontractReceiptDetailMapper;

    private final WmsStockInfoService wmsStockInfoService;

    private final PublicMaterialMapper publicMaterialMapper;

    /**
     * 采购打印明细列表
     *
     * @param id
     * @return
     */
    @Override
    public TableDataInfo<IncomingMaterialPrintingDetailDto> getDetailList(long id, PageQuery pageQuery) throws Exception {
        Page<IncomingMaterialPrintingDetailDto> detailList = incomingMaterialPrintingDetailMapper.getDetailList(pageQuery.build(), id);
        List<IncomingMaterialPrintingDetailDto> records = detailList.getRecords();
        IncomingMaterialPrinting incomingMaterialPrinting = null;
        if(records.size() > 0){
            long printId = records.get(0).getPrintId();
             incomingMaterialPrinting = incomingMaterialPrintingMapper.selectById(printId);

        }
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();
        for (IncomingMaterialPrintingDetailDto entity : records) {
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //单位
            entity.setUnitDict(unitByCodeToMap.get(entity.getMaterialUnit()).getUnitName());
            //转换
            UnitConversionDto unitConversionDto = unitUtil.atWillunitConversion(entity.getConvertUnit(), entity.getMaterialCode(),String.valueOf(entity.getNetWeight()),entity.getMaterialId());
            if (unitConversionDto != null) {
                entity.setUnitConversion(unitConversionDto.getMaterialMoreUnitNumber());
            }
            //采购入库单号
            if(incomingMaterialPrinting != null){
                entity.setReceiptNo(incomingMaterialPrinting.getReceiptNo());
            }

        }
        return TableDataInfo.build(detailList);
    }

    @Override
    public List<IncomingMaterialPrintingDetailDto> getSubcontractList(long id) {
        return null;
    }

    /**
     * 打印（采购）
     *
     * @param incomingMaterialPrintingVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<IncomingMaterialPrintingDetailDto> getPrint(IncomingMaterialPrintingVo incomingMaterialPrintingVo) throws Exception {
        IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectById(incomingMaterialPrintingVo.getId());
        //获取单据信息
        PurchaseReceipt purchaseReceipt = purchaseReceiptMapper.selectOne(new QueryWrapper<PurchaseReceipt>().lambda().eq(PurchaseReceipt::getReceiptNo, incomingMaterialPrinting.getReceiptNo()));
        IncomingMaterialPrintingDto printView = incomingMaterialPrintingService.getPrintView(incomingMaterialPrinting.getId());
        //单位
        MaterialUnitDto data1 = remoteUnitService.getUnitDataByCode(Long.parseLong(printView.getMaterialUnit())).getData();
        String unitQuantity = data1.getUnitQuantity();
        //获取供应商信息
        R<SysApiOrderSupplierManagementDto> view = remoteSysSupplierService.getSupplierByCode(incomingMaterialPrinting.getSupplierCode());
        if (incomingMaterialPrinting.getPrintStatus() == 2) {
            List<IncomingMaterialPrintingDetailDto> detailList = incomingMaterialPrintingDetailMapper.getAllDetailList(incomingMaterialPrintingVo.getId());

            List<PurchaseReceiptDetail> purchaseReceiptDetailList = new ArrayList<>();
            List<WmsStockInfo> wmsStockInfoList = new ArrayList<>();
            for (IncomingMaterialPrintingDetailDto entity : detailList) {
                //获取物料类别信息
                SysApiCategoryDto data = null;
                if (entity.getMaterialType() != 0) {
                    data = remoteCodingService.getByCode(entity.getMaterialCode()).getData();
                }
                //将数据新增至采购入库明细列表
                PurchaseReceiptDetail purchaseReceiptDetail = new PurchaseReceiptDetail();
                purchaseReceiptDetail.setMaterialCode(entity.getMaterialCode())
                    .setMaterialNumber(entity.getMaterialNumber())
                    .setStatus(1)
                    .setGrossWeight(entity.getGrossWeight())
                    .setNetWeight(entity.getNetWeight())
                    .setReceiptQuantity(1)
                    .setBatchNo(entity.getBatchNo())
                    .setDateManufacture(incomingMaterialPrintingVo.getDateManufacture())
                    .setReceiptId(detailList.get(0).getReceiptId());

                purchaseReceiptDetailList.add(purchaseReceiptDetail);
                //新增相关数据到仓库表
                WmsStockInfo wmsStockInfo = new WmsStockInfo();
                wmsStockInfo.setWsiMaterialNumber(purchaseReceiptDetail.getMaterialNumber())
                    .setWsiMaterialCode(entity.getMaterialCode())
                    .setWsiMaterialNum(1)
                    .setWsiMaterialStatus(1)
                    .setWsiMaterialBatch(entity.getBatchNo())
                    .setWsiGorssWeight(entity.getGrossWeight())
                    .setWsiNetWeight(entity.getNetWeight())
                    .setWsiProducedDate(incomingMaterialPrintingVo.getDateManufacture())
                    .setWsiReveiveTime(new Date())
                    .setCreateUser(LoginHelper.getUsername())
                    .setWsiConnectNum(incomingMaterialPrinting.getReceiptNo())
                    .setMaterialUnit(Long.parseLong(printView.getMaterialUnit()))
                    .setCreateTime(new Date());
                if (data != null) {
                    wmsStockInfo.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                    wmsStockInfo.setWsiCategory(String.valueOf(data.getCategoryId()));
                }
                if (unitQuantity != null) {
                    wmsStockInfo.setWsiMinunitNum(Integer.parseInt(unitQuantity));
                }

                if (view != null) {
                    wmsStockInfo.setWsiSupplierCode(view.getData().getSupplierNum());
                }
                wmsStockInfoList.add(wmsStockInfo);
            }
            //修改打印明细
            List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = incomingMaterialPrintingDetailMapper.selectList(new QueryWrapper<IncomingMaterialPrintingDetail>().lambda().eq(IncomingMaterialPrintingDetail::getPrintId, incomingMaterialPrintingVo.getId()));
            for (IncomingMaterialPrintingDetail entry : incomingMaterialPrintingDetails) {
                entry.setDateManufacture(new Date());
            }
            incomingMaterialPrintingDetailMapper.updateBatchById(incomingMaterialPrintingDetails);
            //保存到采购入库明细
            purchaseReceiptDetailMapper.insertBatch(purchaseReceiptDetailList);
            //保存数据到仓库表
            wmsStockInfoMapper.insertBatch(wmsStockInfoList);
            //修改来料打印状态为待入库
            incomingMaterialPrinting.setPrintStatus(3);
            incomingMaterialPrinting.setPrintedQuantity(detailList.size());
            incomingMaterialPrinting.setPrintBy(LoginHelper.getUsername());
            incomingMaterialPrinting.setPrintTime(new Date());
            incomingMaterialPrintingMapper.updateById(incomingMaterialPrinting);
            return detailList;
        } else {
            throw new GlobalException("只能操作待打印的数据");
        }
    }

    /**
     * 批量保存
     *
     * @param list
     * @return
     */
    @Override
    public boolean batchSave(List<IncomingMaterialPrintingDetail> list) {
        return incomingMaterialPrintingDetailMapper.insertBatch(list);
    }

    /**
     * 委外打印
     * @param incomingMaterialPrintingVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<IncomingMaterialPrintingDetailDto> subcontractPrint(IncomingMaterialPrintingVo incomingMaterialPrintingVo) throws Exception {
        IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectById(incomingMaterialPrintingVo.getId());
        //获取单据信息
        PurchaseReceipt purchaseReceipt = purchaseReceiptMapper.selectOne(new QueryWrapper<PurchaseReceipt>().lambda().eq(PurchaseReceipt::getReceiptNo, incomingMaterialPrinting.getReceiptNo()));
        IncomingMaterialPrintingDto printView = incomingMaterialPrintingService.getPrintView(incomingMaterialPrinting.getId());
        //单位
        MaterialUnitDto data1 = remoteUnitService.getUnitDataByCode(Long.parseLong(printView.getMaterialUnit())).getData();
        String unitQuantity = data1.getUnitQuantity();
        //获取供应商信息
//        SysConverterManagementDto converterByCode = remoteSysConverterService.getConverterByCode(incomingMaterialPrinting.getConverterCode());
        if (incomingMaterialPrinting.getPrintStatus() == 2) {
            List<IncomingMaterialPrintingDetailDto> detailList = incomingMaterialPrintingDetailMapper.getAllDetailList(incomingMaterialPrintingVo.getId());

            List<SubcontractReceiptDetail> subcontractReceiptDetailList = new ArrayList<>();
            List<WmsStockInfo> wmsStockInfoList = new ArrayList<>();
            for (IncomingMaterialPrintingDetailDto entity : detailList) {
                //获取物料类别信息
                SysApiCategoryDto data = null;
                if (entity.getMaterialType() != 0) {
                    data = remoteCodingService.getByCode(entity.getMaterialCode()).getData();
                }

                //将委外打印数据保存到委外入库明细列表
                SubcontractReceiptDetail subcontractReceiptDetail = new SubcontractReceiptDetail();
                subcontractReceiptDetail.setMaterialNumber(entity.getMaterialNumber())
                    .setMaterialCode(incomingMaterialPrinting.getMaterialCode())
                    .setStatus(1)
                    .setGrossWeight(entity.getGrossWeight())
                    .setNetWeight(entity.getNetWeight())
                    .setReceiptQuantity(1)
                    .setBatchNo(entity.getBatchNo())
                    .setSubcontractId(detailList.get(0).getReceiptId());
                subcontractReceiptDetailList.add(subcontractReceiptDetail);

                //新增相关数据到仓库表
                WmsStockInfo wmsStockInfo = new WmsStockInfo();
                wmsStockInfo.setWsiMaterialNumber(subcontractReceiptDetail.getMaterialNumber())
                    .setWsiMaterialCode(entity.getMaterialCode())
                    .setWsiMaterialNum(1)
                    .setWsiMaterialStatus(1)
                    .setWsiMaterialBatch(entity.getBatchNo())
                    .setWsiGorssWeight(entity.getGrossWeight())
                    .setWsiNetWeight(entity.getNetWeight())
                    .setWsiProducedDate(incomingMaterialPrintingVo.getDateManufacture())
                    .setWsiReveiveTime(new Date())
                    .setCreateUser(LoginHelper.getUsername())
                    .setWsiConnectNum(incomingMaterialPrinting.getReceiptNo())
                    .setMaterialUnit(Long.parseLong(printView.getMaterialUnit()))
                    .setWsiConverterCode(incomingMaterialPrinting.getConverterCode())
                    .setCreateTime(new Date());
                if (data != null) {
                    wmsStockInfo.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                    wmsStockInfo.setWsiCategory(String.valueOf(data.getCategoryId()));
                }
                if (unitQuantity != null) {
                    wmsStockInfo.setWsiMinunitNum(Integer.parseInt(unitQuantity));
                }

//                if (converterByCode != null) {
//                    wmsStockInfo.setWsiSupplierCode(converterByCode.getSupplierNum());
//                }
                wmsStockInfoList.add(wmsStockInfo);
            }
            //保存到委外入库明细
            subcontractReceiptDetailService.batchSaveList(subcontractReceiptDetailList);
            //修改打印明细
            List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = incomingMaterialPrintingDetailMapper.selectList(new QueryWrapper<IncomingMaterialPrintingDetail>().lambda().eq(IncomingMaterialPrintingDetail::getPrintId, incomingMaterialPrintingVo.getId()));
            for (IncomingMaterialPrintingDetail entry : incomingMaterialPrintingDetails) {
                entry.setDateManufacture(new Date());
            }
            incomingMaterialPrintingDetailMapper.updateBatchById(incomingMaterialPrintingDetails);

            //保存数据到仓库表
            wmsStockInfoMapper.insertBatch(wmsStockInfoList);
            //修改来料打印状态为待入库
            incomingMaterialPrinting.setPrintStatus(3);
            incomingMaterialPrinting.setPrintedQuantity(detailList.size());
            incomingMaterialPrinting.setPrintBy(LoginHelper.getUsername());
            incomingMaterialPrinting.setPrintTime(new Date());
            incomingMaterialPrintingMapper.updateById(incomingMaterialPrinting);
            return detailList;
        } else {
            throw new GlobalException("只能操作待打印的数据");
        }
    }

    /**
     * 来料打印页面对应的号码全部删除，上表的打印状态更新为“已完成”
     *
     * @param deleteDetailVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDetail(DeleteDetailVo deleteDetailVo) {

        //获取明细列表
        List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = incomingMaterialPrintingDetailMapper.selectList(new QueryWrapper<IncomingMaterialPrintingDetail>().lambda().in(IncomingMaterialPrintingDetail::getMaterialNumber, deleteDetailVo.getMaterialNumberList()));
        if (incomingMaterialPrintingDetails.size() > 0) {
            //判断是否采购打印数据
            IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialPrinting>().lambda().eq(IncomingMaterialPrinting::getId, incomingMaterialPrintingDetails.get(0).getPrintId()));
            if (incomingMaterialPrinting.getReceiptNo() != null) {
                //并将明细删除
                incomingMaterialPrintingDetailMapper.deleteBatchIds(incomingMaterialPrintingDetails);
                //同时删除上方单据
                return incomingMaterialPrintingMapper.deleteById(incomingMaterialPrinting) > 0;
//                incomingMaterialPrinting.setPrintStatus(4);
//                return incomingMaterialPrintingService.updateById(incomingMaterialPrinting);

            } else {
                IncomingMaterialSubcontractPrinting incomingMaterialSubcontractPrinting = incomingMaterialSubcontractPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialSubcontractPrinting>().lambda().eq(IncomingMaterialSubcontractPrinting::getId, incomingMaterialPrintingDetails.get(0).getSubcontractId()));
                if (incomingMaterialSubcontractPrinting != null) {
                    //并将明细删除
                    incomingMaterialPrintingDetailMapper.deleteBatchIds(incomingMaterialPrintingDetails);
                    //同时删除上方单据
                    return incomingMaterialSubcontractPrintingMapper.deleteById(incomingMaterialSubcontractPrinting) > 0;
//                    incomingMaterialSubcontractPrinting.setPrintStatus(4);
//                    return incomingMaterialSubcontractPrintingService.updateById(incomingMaterialSubcontractPrinting);
                }
            }
        }
        return false;
    }

    /**
     * 删除来料打印页面数据（fegin调用）
     *
     * @param deleteDetailVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteData(DeleteDetailVo deleteDetailVo) {

        //获取明细列表
        List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = incomingMaterialPrintingDetailMapper.selectList(new QueryWrapper<IncomingMaterialPrintingDetail>().lambda().in(IncomingMaterialPrintingDetail::getMaterialNumber, deleteDetailVo.getMaterialNumberList()));
        if (incomingMaterialPrintingDetails.size() > 0) {
            //判断是否采购打印数据
            IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialPrinting>().lambda().eq(IncomingMaterialPrinting::getId, incomingMaterialPrintingDetails.get(0).getPrintId()));
            if (incomingMaterialPrinting.getReceiptNo() != null) {
                //并将明细删除
                return incomingMaterialPrintingDetailMapper.deleteBatchIds(incomingMaterialPrintingDetails) > 0;
//                return incomingMaterialPrintingMapper.deleteById(incomingMaterialPrinting) > 0;

            } else {
                IncomingMaterialSubcontractPrinting incomingMaterialSubcontractPrinting = incomingMaterialSubcontractPrintingMapper.selectOne(new QueryWrapper<IncomingMaterialSubcontractPrinting>().lambda().eq(IncomingMaterialSubcontractPrinting::getId, incomingMaterialPrintingDetails.get(0).getSubcontractId()));
                if (incomingMaterialSubcontractPrinting != null) {
                    //并将明细删除
                    incomingMaterialPrintingDetailMapper.deleteBatchIds(incomingMaterialPrintingDetails);
                    return incomingMaterialSubcontractPrintingMapper.deleteById(incomingMaterialSubcontractPrinting) > 0;
                }
            }
        }
        return false;
    }

    /**
     * 批量修改
     *
     * @param list
     * @return
     */
    @Override
    public boolean batchUpdate(List<IncomingMaterialPrintingDetail> list) {
        return incomingMaterialPrintingDetailMapper.updateBatchById(list);
    }

    /**
     * 完工打印
     * @param incomingMaterialPrintingVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<IncomingMaterialPrintingDetailDto> wipCompletionPrint(IncomingMaterialPrintingVo incomingMaterialPrintingVo) throws Exception {
        IncomingMaterialPrinting incomingMaterialPrinting = incomingMaterialPrintingMapper.selectById(incomingMaterialPrintingVo.getId());
        //获取单据信息
        PurchaseReceipt purchaseReceipt = purchaseReceiptMapper.selectOne(new QueryWrapper<PurchaseReceipt>().lambda().eq(PurchaseReceipt::getReceiptNo, incomingMaterialPrinting.getReceiptNo()));
        IncomingMaterialPrintingDto printView = incomingMaterialPrintingService.getPrintView(incomingMaterialPrinting.getId());
        //单位
        MaterialUnitDto data1 = remoteUnitService.getUnitDataByCode(Long.parseLong(printView.getMaterialUnit())).getData();
        String unitQuantity = data1.getUnitQuantity();
        //获取供应商信息
//        SysConverterManagementDto converterByCode = remoteSysConverterService.getConverterByCode(incomingMaterialPrinting.getConverterCode());
        if (incomingMaterialPrinting.getPrintStatus() == 2) {
            List<IncomingMaterialPrintingDetailDto> detailList = incomingMaterialPrintingDetailMapper.getAllDetailList(incomingMaterialPrintingVo.getId());

            List<WipCompletionDetail> wipCompletionDetailList = new ArrayList<>();
            List<WmsStockInfo> wmsStockInfoList = new ArrayList<>();
            for (IncomingMaterialPrintingDetailDto entity : detailList) {
                //获取物料类别信息
                SysApiCategoryDto data = null;
                if (entity.getMaterialType() != 0) {
                    data = remoteCodingService.getByCode(entity.getMaterialCode()).getData();
                }

                //将委外打印数据保存到完工入库明细列表
                WipCompletionDetail wipCompletionDetail = new WipCompletionDetail();
                wipCompletionDetail.setMaterialNumber(entity.getMaterialNumber())
                    .setMaterialCode(incomingMaterialPrinting.getMaterialCode())
                    .setStatus(1)
                    .setGrossWeight(entity.getGrossWeight())
                    .setNetWeight(entity.getNetWeight())
                    .setReceiptQuantity(1)
                    .setBatchNo(entity.getBatchNo())
                    .setWipCompletionId(detailList.get(0).getReceiptId());
                wipCompletionDetailList.add(wipCompletionDetail);

                //新增相关数据到仓库表
                WmsStockInfo wmsStockInfo = new WmsStockInfo();
                wmsStockInfo.setWsiMaterialNumber(wipCompletionDetail.getMaterialNumber())
                    .setWsiMaterialCode(entity.getMaterialCode())
                    .setWsiMaterialNum(1)
                    .setWsiMaterialStatus(1)
                    .setWsiMaterialBatch(entity.getBatchNo())
                    .setWsiGorssWeight(entity.getGrossWeight())
                    .setWsiNetWeight(entity.getNetWeight())
                    .setWsiProducedDate(incomingMaterialPrintingVo.getDateManufacture())
                    .setWsiReveiveTime(new Date())
                    .setCreateUser(LoginHelper.getUsername())
                    .setWsiConnectNum(incomingMaterialPrinting.getReceiptNo())
                    .setMaterialUnit(Long.parseLong(printView.getMaterialUnit()))
                    .setWsiConverterCode(incomingMaterialPrinting.getConverterCode())
                    .setCreateTime(new Date());
                if (data != null) {
                    wmsStockInfo.setWsiMaterialType(Long.parseLong(data.getCategoryType()));
                    wmsStockInfo.setWsiCategory(String.valueOf(data.getCategoryId()));
                }
                if (unitQuantity != null) {
                    wmsStockInfo.setWsiMinunitNum(Integer.parseInt(unitQuantity));
                }

//                if (converterByCode != null) {
//                    wmsStockInfo.setWsiSupplierCode(converterByCode.getSupplierNum());
//                }
                wmsStockInfoList.add(wmsStockInfo);
            }
            //保存到委外入库明细
            wipCompletionDetailService.saveBatchWipCompletionDetail(wipCompletionDetailList);
            //修改打印明细
            List<IncomingMaterialPrintingDetail> incomingMaterialPrintingDetails = incomingMaterialPrintingDetailMapper.selectList(new QueryWrapper<IncomingMaterialPrintingDetail>().lambda().eq(IncomingMaterialPrintingDetail::getPrintId, incomingMaterialPrintingVo.getId()));
            for (IncomingMaterialPrintingDetail entry : incomingMaterialPrintingDetails) {
                entry.setDateManufacture(new Date());
            }
            incomingMaterialPrintingDetailMapper.updateBatchById(incomingMaterialPrintingDetails);

            //保存数据到仓库表
            wmsStockInfoMapper.insertBatch(wmsStockInfoList);
            //修改来料打印状态为待入库
            incomingMaterialPrinting.setPrintStatus(3);
            incomingMaterialPrinting.setPrintedQuantity(detailList.size());
            incomingMaterialPrinting.setPrintBy(LoginHelper.getUsername());
            incomingMaterialPrinting.setPrintTime(new Date());
            incomingMaterialPrintingMapper.updateById(incomingMaterialPrinting);
            return detailList;
        } else {
            throw new GlobalException("只能操作待打印的数据");
        }
    }
}
